var classGrades = new Array(); //存放所有班级的数组

// 创建班级的构造方法
function classGrade(id, classGradeName, grade, teacherName, slogan, studentNumber) {
    this.id = id;
    this.classGradeName = classGradeName;
    this.grade = grade;
    this.teacherName = teacherName;
    this.slogan = slogan;
    this.studentNumber = studentNumber;
}

// 刷新表格
function refresh(classGrades) {
    var tbody = document.getElementById("tbody");
    var html = "";
    var color = "warning";
    var flag = true;
    for (var i in classGrades) {
        if (flag) {
            color = "info";
        } else {
            color = "warning";
        }
        html = html + "<tr class='" + color + "'><td style='width:30px;'><input type='checkbox'></td><td id='classId'>"
            + classGrades[i].id + "</td><td id='classGradeName'>"
            + classGrades[i].classGradeName + "</td><td id='grade'>"
            + classGrades[i].grade + "</td><td id='teacherName'>"
            + classGrades[i].teacherName + "</td><td id='slogan'>"
            + classGrades[i].slogan + "</td><td id='studentNumber'>"
            + classGrades[i].studentNumber + "</td>"
            + "</tr>";
        flag = !flag;//颜色转换
    }
    tbody.innerHTML = html; // 在表格中创建所有tr元素
}

// 要删除的id
let delids = "";
//删除班级
function del() {
    var checkboxs = $("input[type='checkbox']");  // 获取所有复选框
    let f = false;  // 是否选中班级的标记
    for (var i = 0; i < checkboxs.length; i++) {
        var box = checkboxs[i];
        if (box.checked) {  // 某个复选框被选中
            f = true;  // 标记 有 复选框被选中
            box = $(box); // jsdom对象转换成jquery对象
            let code = box.parent().siblings("#classId").text(); // 复选框父级的兄弟#code元素
            delids = delids + ";" + code;
        }
    }
    // button.innerHTML = html;
    if(!f){
        alert("请选择一个班级！");
    }else {
        location.href = "deleteClassGradeServlet?ids=" + delids;
        // alert("删除成功！")
    }
}

var classId; //被修改的班级编号
//编辑班级
function edit() {
    var checkboxs = $("input[type='checkbox']");
    let f = false;
    for (var i = 0; i < checkboxs.length; i++) {
        var box = checkboxs[i];
        if (box.checked) {
            f = true;
            $(this).attr("data-toggle","modal");  // 启用模态框
            $(this).attr("data-target","#myModal"); // 启用模态框
            box = $(box);
            classId = box.parent().siblings("#classId").text();
            // 获取表格中选中的班级的各项数据
            var classGradeName = box.parent().siblings("#classGradeName").text();
            var grade = box.parent().siblings("#grade").text();
            var teacherName = box.parent().siblings("#teacherName").text();
            var slogan = box.parent().siblings("#slogan").text();
            var studentNumber = box.parent().siblings("#studentNumber").text();
            // 选中班级的各项数据，放在模态框的相应input框中
            $("#class_id").val(classId);
            $("#class_name").val(classGradeName);
            $("#class_grade").val(grade);
            $("#teacher_name").val(teacherName);
            $("#class_slogan").val(slogan);
            $("#student_number").val(studentNumber);
        }
    }
    if(!f){
        alert("请选择一个班级！");
        $(this).attr("data-toggle",null); // 禁用模态框
        $(this).attr("data-target",null); // 禁用模态框
    }
}


